using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using ResourceBasedAuthApp.CustomRoleAuthorization;
using ResourceBasedAuthApp.Services;
using System.Text;

namespace ResourceBasedAuthApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "First web Api", Version = "v1" });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = @"JWT Authorization header using the Bearer scheme.
                      Enter 'Bearer' [space] and then your token in the text input below.
                      Example: 'Bearer 12345abcdef'",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                 {
                   {
                             new OpenApiSecurityScheme
                             {
                               Reference = new OpenApiReference
                                 {
                                   Type = ReferenceType.SecurityScheme,
                                   Id = "Bearer"
                                 },
                                 Scheme = "oauth2",
                                 Name = "Bearer",
                                 In = ParameterLocation.Header,
                               },
                               new List<string>()
                             }
                  });

            });
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtOptions => {
                jwtOptions.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = "myToken",
                    ValidAudience = "aaaa",
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("qwertyuiop123456"))
                };

            });

  
            //builder.Services.AddAuthorization(options => {
            //    options.AddPolicy("EditPolicy", policy =>
            //            policy.Requirements.Add(new SameAuthorRequirement()));
            //});

            //builder.Services.AddSingleton<IAuthorizationHandler, DocumentAuthorizationHandler>();
            //builder.Services.AddSingleton<IAuthorizationHandler, DocumentAuthorizationCrudHandler>();


            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("CustomRolePolicy", policy =>
                {
                    policy.Requirements.Add(new CustomRoleRequirement());
                });
            });
            builder. Services.AddSingleton<IAuthorizationHandler, CustomRoleAuthorizationHandler>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {

            }
     
            app.UseSwagger();
            app.UseSwaggerUI();

            app.UseAuthentication();
            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}